package com.green.client;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.media.client.Audio;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.IconButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.IconMenuButton;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.toolbar.RibbonBar;
import com.smartgwt.client.widgets.toolbar.RibbonGroup;

import fr.hd3d.html5.video.client.VideoSource;
import fr.hd3d.html5.video.client.VideoSource.VideoType;
import fr.hd3d.html5.video.client.VideoWidget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Korean implements EntryPoint {

	/**
     * Creates a new instance of greenEntryPoint
     */
    private static final String GREETING_TEXT = "Hallo David, our recommendation for your German";
    private static final String REVIEW_TEXT = "Start your Review Manager and practice 7 items.";

    Logger logger = Logger.getLogger("console");
    /**
     * The entry point method, called automatically by loading a module that declares an implementing class as an entry-point
     */
    public void onModuleLoad() {

        logger.log(Level.INFO, "OnModuleLoad has been called.");

        RibbonBar ribbonBar = new RibbonBar();

        ribbonBar.setLeft(0);
        ribbonBar.setTop(15);
        ribbonBar.setWidth100();

        ribbonBar.setMembersMargin(2);
        ribbonBar.setLayoutMargin(2);

        Menu menu = new Menu();

        menu.addItem(new MenuItem("Document", "icons/16/document_plain_new.png", "Ctrl+D"));
        menu.addItem(new MenuItem("Picture", "icons/16/folder_out.png", "Ctrl+P"));
        menu.addItem(new MenuItem("Email", "icons/16/disk_blue.png", "Ctrl+E"));

        // Course Group
        RibbonGroup courseGroup = new RibbonGroup();
        courseGroup.setTitle("Course");
        courseGroup.setTitleAlign(Alignment.LEFT);
        courseGroup.setNumRows(3);
        courseGroup.setRowHeight(26);
        courseGroup.addControl(getIconMenuButton("Start", "piece_blue", menu, false));
        courseGroup.addControl(getIconButton("Beginner", "star_yellow", false));
        courseGroup.addControl(getIconButton("Words", "pawn_red", false));
        courseGroup.addControl(getIconMenuButton("Grammar", "cube_green", menu, false));
        courseGroup.addControl(getIconMenuButton("Business", "piece_blue", menu, false));

        RibbonGroup editGroup = new RibbonGroup();
        editGroup.setTitle("Edit");
        editGroup.setNumRows(3);
        editGroup.setRowHeight(26);
        editGroup.addControl(getIconButton("Edit", "piece_blue", false));
        editGroup.addControl(getIconButton("Copy", "pawn_green", false));
        editGroup.addControl(getIconButton("Paste", "cube_yellow", false));
        editGroup.addControl(getIconMenuButton("Undo", null, menu, false));
        editGroup.addControl(getIconMenuButton("Redo", null, menu, false));

        RibbonGroup insertGroup = new RibbonGroup();
        insertGroup.setTitle("Insert");
        insertGroup.setNumRows(3);
        insertGroup.setRowHeight(26);
        insertGroup.addControl(getIconMenuButton("Picture", null, menu, true));
        insertGroup.addControl(getIconButton("Link", "pawn_white", false));
        insertGroup.addControl(getIconButton("Document", "star_yellow", false));
        insertGroup.addControl(getIconButton("Video", "piece_red", false));

        ribbonBar.addMember(courseGroup);
        ribbonBar.addMember(editGroup);
        ribbonBar.addMember(insertGroup);

        ribbonBar.setTop(0);

        // ribbonBar.draw();

        Canvas canvas = new Canvas();

        VLayout vLayout1st = new VLayout();
        vLayout1st.setTop(0);
        vLayout1st.setShowEdges(false);
        vLayout1st.setWidth(1000);
        vLayout1st.setMembersMargin(5);
        vLayout1st.setLayoutMargin(10);
        vLayout1st.addMember(ribbonBar);




        //saiimple explanation with review  button
        HLayout hLayout1st = new HLayout();
        //hLayout1st.setTop(0);
        hLayout1st.setShowEdges(true);
        hLayout1st.setWidth(800);
        hLayout1st.setMembersMargin(5);
        hLayout1st.setLayoutMargin(10);

        //Review Text
        final Label reviewLabel = new Label(REVIEW_TEXT);
        reviewLabel.setWidth(500);
        reviewLabel.setHeight(10);
        reviewLabel.setPadding(10);
        reviewLabel.setLeft(0);
        reviewLabel.setTop(20);
        reviewLabel.setBackgroundColor("white");
        reviewLabel.setBorder("1px solid #c0c0c0");
        reviewLabel.setCanDragReposition(true);
        reviewLabel.setShowShadow(false);
        reviewLabel.setShadowSoftness(10);
        reviewLabel.setShadowOffset(5);
        reviewLabel.setKeepInParentRect(true);

        //Review Button
        final IButton reviewButton = new IButton("Review Now");
        reviewButton.setWidth(150);
        reviewButton.setShowRollOver(true);
        reviewButton.setShowDisabled(true);
        reviewButton.setShowDown(true);
        reviewButton.setTitleStyle("stretchTitle");
        reviewButton.setIcon("silk/printer.png");

        reviewButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {

                //works chorome but not ie
            	logger.log(Level.INFO, "Start auido Test");
                Audio audioTest = Audio.createIfSupported();
                if (audioTest != null) {
                	logger.log(Level.INFO, "Listen Audio");
                    audioTest.setSrc("audio/chimes.wav");
                    audioTest.play();
                }

                //Test Video
                
                VideoWidget videoPlayer = new VideoWidget(true, false, null);

                List<VideoSource> sources = new ArrayList<VideoSource>();
                sources.add(new VideoSource("fr_hd3d_html5_video_demo/videos/bbb_trailer_400p.ogv"));
                sources.add(new VideoSource("fr_hd3d_html5_video_demo/videos/bbb_trailer_360p.webm", VideoType.WEBM));


                    videoPlayer.setSources(sources);
                    videoPlayer.setPixelSize(960, 640);
                    RootPanel.get().add(videoPlayer);
                    
                    
                //works chorome but not ie
//                SoundController soundController = new SoundController();
//                Sound sound = soundController.createSound(Sound.MIME_TYPE_AUDIO_BASIC,
//                        "audio/chimes.wav");
//                if (sound != null) {
//                    Window.alert("sound supported");
//                    sound.play();
//                }


            }
        });



        hLayout1st.addMember(reviewLabel);
        hLayout1st.addMember(reviewButton);

        //greetings
        final Label label = new Label(GREETING_TEXT);
        label.setWidth(800);
        label.setHeight(10);
        label.setPadding(10);
        label.setLeft(160);
        label.setTop(20);
        label.setBackgroundColor("white");
        label.setBorder("1px solid #c0c0c0");
        label.setCanDragReposition(true);
        label.setShowShadow(false);
        label.setShadowSoftness(10);
        label.setShadowOffset(5);
        label.setKeepInParentRect(true);

        vLayout1st.addMember(label);
        vLayout1st.addMember(hLayout1st);



//        vLayout1st.addMember(new BlueBox(null, 40, "Hallo David, our recommendation for your German"));
//        vLayout1st.addMember(new BlueBox(null, 80, "height 80"));
//        vLayout1st.addMember(new BlueBox(null, 120, "height 120"));
//        vLayout1st.addMember(new BlueBox(null, 160, "height 160"));

//		canvas.addChild(ribbonBar);
        canvas.addChild(vLayout1st);
        RootPanel mainPanel = RootPanel.get("main");
        mainPanel.add(canvas);

        canvas.draw();



    }

    private IconButton getIconButton(String title, String iconName, boolean vertical) {
        IconButton button = new IconButton(title);
        button.setTitle(title);
        if (iconName == null) {
            iconName = "cube_blue";
        }
        button.setIcon("pieces/16/" + iconName + ".png");
        button.setLargeIcon("pieces/48/" + iconName + ".png");
        if (vertical == true) {
            button.setOrientation("vertical");
        }
        return button;
    }

    /**
     * Get icon
     *
     * @param title
     * @param iconName
     * @param menu
     * @param vertical if true, big one
     * @return
     */
    private IconMenuButton getIconMenuButton(String title, String iconName, Menu menu, boolean vertical) {
        IconMenuButton button = new IconMenuButton();
        button.setTitle(title);
        if (iconName == null) {
            iconName = "cube_blue";
        }
        button.setIcon("pieces/16/" + iconName + ".png");
        button.setLargeIcon("pieces/48/" + iconName + ".png");
        if (vertical == true) {
            button.setOrientation("vertical");
        }
        if (menu != null) {
            button.setMenu(menu);
        }

        button.setShowMenuIcon(true);
        return button;
    }

    public void onModuleLoadDefault() {
        final Label label = new Label("Hello, GWT!!!");
        final Button button = new Button("Click me!");
        Logger logger = Logger.getLogger("GWT101 Log");
        logger.log(Level.INFO, "OnModuleLoad has been called.");


        RootPanel.get().add(button);
        RootPanel.get().add(label);
    }
}